//
//  LBCategoryCell.m
//  jslj
//
//  Created by luckyRoy on 2017/10/10.
//  Copyright © 2017年 广东联结电子商务有限公司. All rights reserved.
//

#import "LBCategoryCell.h"
#import "Masonry.h"
#import "LBCategoryModel.h"
#import "LBCategorySelectedChildView.h"
#import <UIColor+LBColor.h>

static CGFloat const LBCategoryCellLabelHeight = 70.0;

@interface LBCategoryCell ()

@property (strong, nonatomic) UIView *unSelectedBgView;

@property (strong, nonatomic) UILabel *titleLabel;

@property (nonatomic, strong) LBCategorySelectedChildView *selectedView;

@end

@implementation LBCategoryCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self createAllView];
    }
    return self;
}

- (void)createAllView
{
    [self.contentView addSubview:self.unSelectedBgView];
    [self.contentView addSubview:self.selectedView];
    
    [self.unSelectedBgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(5);
        make.right.offset(-5);
        make.bottom.offset(-5);
        make.top.offset(5);
    }];
    [self.selectedView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(5);
        make.right.offset(-5);
        make.bottom.offset(-5);
        make.top.offset(5);
    }];
}

#pragma mark - setter

- (void)fac_updateUIViewByModel:(id)cellModel
{
    LBCategoryModel *theCellModel = cellModel;
    
    [self.unSelectedBgView setHidden:YES];
    [self.selectedView setHidden:YES];
    
    if (theCellModel.isChosedCell) {
        [self.selectedView setHidden:NO];
        NSString *showTitle = theCellModel.name;
        [self.selectedView refreshUIByIsEdiotr:theCellModel.isEdiotr newTitle:showTitle];
    } else {
        [self.unSelectedBgView setHidden:NO];
        [self.titleLabel setText:theCellModel.name];
        if (theCellModel.isAll) {
            [self.titleLabel setText:@"全部"];
        }
        if (theCellModel.isAllAll) {
            [self.titleLabel setText:@"全部分类"];
        }
    }
    
    if (theCellModel.isSelected) {
        self.unSelectedBgView.backgroundColor = [UIColor colorWithRed:244/255.0 green:250/255.0 blue:255/255.0 alpha:1];
        [self.titleLabel setTextColor:[UIColor blue]];
    } else {
        self.unSelectedBgView.backgroundColor = [UIColor whiteColor];
        [self.titleLabel setTextColor:[UIColor darkBlack]];
    }
}



#pragma mark - getter

- (UIView *)unSelectedBgView
{
    if (!_unSelectedBgView) {
        _unSelectedBgView = [[UIView alloc]init];
        
        [self.unSelectedBgView addSubview:self.titleLabel];
        
        [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.mas_equalTo(self.unSelectedBgView);
        }];
        
        _unSelectedBgView.layer.cornerRadius = 3;
        _unSelectedBgView.layer.borderColor = [UIColor gray].CGColor;
        _unSelectedBgView.layer.borderWidth = 0.5;
    }
    return _unSelectedBgView;
}

- (UILabel *)titleLabel
{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc]init];
        _titleLabel.textColor = [UIColor darkBlack];
        _titleLabel.font = [UIFont systemFontOfSize:13];
        _titleLabel.numberOfLines = 2;
        _titleLabel.textAlignment = NSTextAlignmentCenter;
        _titleLabel.backgroundColor = [UIColor clearColor];
    }
    return _titleLabel;
}

- (LBCategorySelectedChildView *)selectedView
{
    if (!_selectedView) {
        _selectedView = [[LBCategorySelectedChildView alloc]initWithTitle:@"" complete:nil];
    }
    return _selectedView;
}

+ (CGFloat)fac_heightForViewByModel:(id)cellModel
{
    CGFloat totalHeight = 0;
    
    LBCategoryModel *theCellModel = cellModel;
    
    if (theCellModel.isChosedCell) {
        totalHeight += [LBCategorySelectedChildView sizeForViewByModel:theCellModel].height;
    } else {
        totalHeight += LBCategoryCellLabelHeight;
    }
    
    return totalHeight;
}

@end
